<template>
  <div class="container">
    <div class="loginFormWangteng">
      <div class="loginLogo">
        <el-image :src="require('@/assets/img/wtlogo.png')" style="width: 32px;height:32px;margin-right:10px;" fit="cover"></el-image>
        <div>{{$t('工控防火墙')}}</div>
      </div>
      <div class="loginText">
        {{$t('用户登录')}}
      </div>
      <el-form label-width="" :model="ruleForm" ref="ruleForm" :rules="loginRules" class="loginFormItem">
        <el-form-item label="" prop="username">
          <el-input v-emoji v-model="ruleForm.username" size="medium" :placeholder="$t('用户名2')"></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input v-emoji type="password" size="medium" :placeholder="$t('密码')" v-model="ruleForm.password" @keyup.enter.native="submitForm(ruleForm)"></el-input>
        </el-form-item>
        <el-button type="warning" class="loginButton loginWangtengButton" :loading="loading" size="small" @click.native.prevent="submitForm(ruleForm)">
          <svg-icon icon-class="login_right" style="font-size:40px;line-height: 48px;" />
        </el-button>
        <div style="text-align: right;margin-top: 2px;">
          <el-link :underline="false" style="color: #666;font-size: 12px;" @click="userAuthLoginClick">{{$t('用户认证')}}<i class="el-icon-arrow-right el-icon--right"></i> </el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { setRoleList } from '@/utils/auth'
import { getMenu } from '@/api/menu'
export default {
  name: 'LoginWangteng',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入用户名')))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入密码')))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      loading: false,
      loginRules: {
        username: [{ required: false, trigger: 'blur', validator: validateUsername }],
        password: [{ required: false, trigger: 'blur', validator: validatePassword }]
      }
    }
  },
  methods: {
    submitForm (ruleForm) {
      let _that = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', ruleForm).then(res => {
            if (res.status === 0) {
              if (res.info[0].ukey !== 0) {
                this.OnLoadRead().then(r => {
                  setTimeout(function () {
                    _that.Arm_Enum_Read([res.info[0], _that.goMenu, _that.colseLoading])
                  }, 100)
                })
              } else {
                this.$store.dispatch('user/saveInfo', res.info[0]).then(() => {
                  getMenu({ roleID: res.info[0].roleID }).then((response) => {
                    if (response.status === 0) {
                      setRoleList(response.url_list)
                      this.$nextTick(() => {
                        this.loading = false
                        if (res.info[0].password_update_time === 0) {
                          this.$router.push('/changePassword')
                        } else {
                          this.$router.push('/')
                        }
                      })
                    }
                  }).catch(e => {
                    this.loading = false
                  })
                })
              }
            } else {
              this.loading = false
            }
          }).catch(e => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
        }
      })
    },
    goMenu (info, status) {
      if (status !== 0) {
        this.loading = false
        this.$store.dispatch('user/logout')
        this.$messages.error(this.$t('UkeyAgain'))
      } else {
        this.$store.dispatch('user/saveInfo', info).then(() => {
          getMenu({ role: info.role }).then((response) => {
            if (response.status === 0) {
              setRoleList(response.url_list)
              this.$nextTick(() => {
                this.loading = false
                if (info.password_update_time === 0) {
                  this.$router.push('/changePassword')
                } else {
                  this.$router.push('/')
                }
              })
            }
          }).catch(e => {
            this.loading = false
          })
        })
      }
    },
    colseLoading () {
      this.loading = false
    },
    userAuthLoginClick () {
      const hrefUrl = window.location.href.match(/(http:\/\/|https:\/\/)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/)
      const url = hrefUrl[0] + ':8183/#/authLogin'
      window.open(url)
    }
  }
}
</script>

<style scoped>
.container {
  position: relative;
  width: 100%;
  height: 100vh;
  box-sizing: border-box;
  z-index: 1;
  background: url("../../assets/img/wangteng.png") no-repeat center fixed;
  background-size: cover;
}
.loginFormWangteng {
  position: absolute;
  left: 25%;
  top: 47.5%;
  transform: translate(-50%, -50%);
  width: 430px;
  height: 427px;
  padding: 48px 49px;
  border-radius: 10px;
  z-index: 1001;
  /* border: 2px solid rgba(255, 255, 255, 0.6); */
  /* background-color: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px); */
  background-color: rgba(255, 255, 255, 1);
}
.loginFormItem {
  width: 332px;
}
.loginLogo {
  font-size: 26px;
  color: #303133;
  height: 40px;
  line-height: 40px;
  /* font-weight: 600; */
  margin-bottom: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  /* letter-spacing: 1px; */
  font-family: PingFangC;
}
.loginText {
  width: 100%;
  font-size: 20px;
  color: #17243e;
  margin-bottom: 24px;
  font-family: PingFangM;
}
.loginPhoto {
  position: absolute;
  left: 8%;
  z-index: 1001;
  top: 10%;
}
.loginButton {
  width: 100%;
  margin-top: 20px;
  border: none;
  z-index: 10;
}
.textCompany {
  position: absolute;
  z-index: 1001;
  transform: translate(-50%, -50%);
  left: 50%;
  bottom: 0.1%;
  color: #337ab7;
}
</style>
<style lang='scss'>
.loginFormWangteng {
  font-family: PingFangM;
  .el-form-item__label {
    line-height: 36px;
    font-size: 14px;
  }
  .el-form-item {
    margin-bottom: 16px;
  }
  .el-input__inner::placeholder {
    font-family: PingFangM;
  }
  .el-button--warning {
    height: 48px;
    background-color: #f26521;
    padding: 0;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  }
  .loginWangtengButton::before {
    width: 332px;
    height: 48px;
    margin-left: 1px;
    margin-top: 1px;
  }
  .loginWangtengButton .el-icon-loading {
    display: none;
  }
}
</style>
